package com.returnsoft.security.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;

import com.returnsoft.junit.Util;
import com.returnsoft.security.dto.PermissionDto;
import com.returnsoft.security.dto.UserDto;
import com.returnsoft.security.exception.SecurityExcepcion;
import com.returnsoft.security.service.UserInterface;

@ManagedBean
@RequestScoped
public class LoginController {

	private String username;
	private String password;

	private final String PERMISSION_SECURITY_USER = "security_user";
	private final String PERMISSION_SECURITY_ADMIN = "security_admin";
	private final List<String> PERMISSIONS = new ArrayList<String>();

	public LoginController() {
		PERMISSIONS.add(PERMISSION_SECURITY_USER);
		PERMISSIONS.add(PERMISSION_SECURITY_ADMIN);
	}

	public String doLogin() {

		try {
			// INICIALIZA BEAN
			UserInterface userService = (UserInterface) new InitialContext(
					Util.getInitProperties())
					.lookup("java:global/SecurityEAR/SecurityEJB/UserBean"
							+ "!com.returnsoft.security.service.UserInterface");

			// BUSCA USUARIO Y CLAVE
			UserDto user = userService.doLogin(username, password, PERMISSIONS);

			// SE GUARDA LA SESSION
			SessionBean session = new SessionBean();
			session.setUserId(user.getId());
			session.setIsAdmin(Boolean.FALSE);

			List<PermissionDto> permissions = user.getPermissions();

			for (PermissionDto permissionDto : permissions) {
				if (permissionDto.getCode().equals(PERMISSION_SECURITY_ADMIN)) {
					session.setIsAdmin(Boolean.TRUE);
				}
			}

			session.setName(user.getPerson().getFirstname() + " "
					+ user.getPerson().getLastname());
			session.setUsername(username);

			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().put("sessionBean", session);

			return "security.xhtml";

		} catch (Exception e) {
			if (!(e instanceof SecurityExcepcion)) {
				e.printStackTrace();
			}
			FacesMessage msg = new FacesMessage(e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return null;
		}

	}

	public String doLogout() {
		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.invalidateSession();

			return "login";
		} catch (Exception e) {
			if (!(e instanceof SecurityExcepcion)) {
				e.printStackTrace();
			}
			FacesMessage msg = new FacesMessage(e.getMessage(), e.getMessage());
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return null;
		}

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
